package com.dianxin.smartpark.service.common.impl;

import java.util.Date;
import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkAccessTime;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkAccessTimeExample;
import com.dianxin.smartpark.dao.mapper.common.SmartParkAccessTimeMapper;
import com.dianxin.smartpark.dao.qo.common.SmartParkAccessTimeQO;
import com.dianxin.smartpark.service.common.SmartParkAccessTimeService;
import com.dianxin.smartpark.service.vo.common.SmartParkAccessTimeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 授权时间段管理业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-28
 */
@Service
public class SmartParkAccessTimeServiceImpl implements SmartParkAccessTimeService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkAccessTimeServiceImpl.class);
    @Resource
    private SmartParkAccessTimeMapper smartParkAccessTimeMapper;

    /**
     * 查询授权时间段管理
     * 
     * @param qo 授权时间段管理ID
     * @return 授权时间段管理
     */
    @Override
    public CommonResponseBody<SmartParkAccessTimeVO> selectSmartParkAccessTime(SmartParkAccessTimeQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkAccessTimeExample smartParkAccessTimeExample = new SmartParkAccessTimeExample();
        SmartParkAccessTimeExample.Criteria criteria = smartParkAccessTimeExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkAccessTime> passRecords = smartParkAccessTimeMapper.selectByExample(smartParkAccessTimeExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkAccessTimeVO.class);
    }

    /**
     * 查询授权时间段管理列表
     * 
     * @param qo 授权时间段管理
     * @return 授权时间段管理
     */
    @Override
    public CommonResponseBody<List<SmartParkAccessTimeVO>> selectSmartParkAccessTimeList(SmartParkAccessTimeQO qo)
    {
        SmartParkAccessTimeExample smartParkAccessTimeExample = new SmartParkAccessTimeExample();
        SmartParkAccessTimeExample.Criteria criteria = smartParkAccessTimeExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getUserId() != null) {
            criteria.andUserIdEqualTo(qo.getUserId());
        }

        if (qo.getUserType() != null) {
            criteria.andUserTypeEqualTo(qo.getUserType());
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkAccessTimeExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkAccessTimeExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkAccessTime> passRecords = smartParkAccessTimeMapper.selectByExample(smartParkAccessTimeExample);
        long count = smartParkAccessTimeMapper.countByExample(smartParkAccessTimeExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkAccessTimeVO.class);
    }

    /**
     * 新增授权时间段管理
     * 
     * @param vo 授权时间段管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkAccessTime(SmartParkAccessTimeVO vo)
    {
        // 验证 todo
        SmartParkAccessTime smartParkAccessTime = new SmartParkAccessTime();
        try {
            BeanUtil.insertConversion(vo, smartParkAccessTime);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkAccessTime.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkAccessTimeMapper.insertSelective(smartParkAccessTime));
    }

    /**
     * 修改授权时间段管理
     * 
     * @param vo 授权时间段管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkAccessTime(SmartParkAccessTimeVO vo)
    {
        // 验证 todo
        SmartParkAccessTime smartParkAccessTime = new SmartParkAccessTime();
        try {
            BeanUtil.insertConversion(vo, smartParkAccessTime);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkAccessTime.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkAccessTimeMapper.updateByPrimaryKeySelective(smartParkAccessTime));
    }

    /**
     * 删除授权时间段管理信息
     * 
     * @param vo 授权时间段管理
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkAccessTime(SmartParkAccessTimeVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkAccessTime smartParkAccessTime = new SmartParkAccessTime();
        try {
            BeanUtil.insertConversion(vo, smartParkAccessTime);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkAccessTime.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkAccessTimeMapper.updateByPrimaryKeySelective(smartParkAccessTime));
    }
}
